<?php
class MessengerController extends Controller
{
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
	public function updateLastLogin($id)
	{
		$sql= 'UPDATE user SET lastlogin=NOW() WHERE id='.$id;
		$query=Yii::app()->db->createCommand($sql)->execute();
		return $query;
	}

	public function isOnline($id)
	{
		$sql='SELECT id,lastlogin FROM user WHERE id="'.$id.'"';
		$query=Yii::app()->db->createCommand($sql)->queryAll();

		if(!isset($query[0]['lastlogin']))
		{
			return false;
		}
		else
		{
			$lastlogin = $query[0]['lastlogin'];
			$lastlogin = explode(' ',$lastlogin);
			$lastlogin['date'] = $lastlogin['0'];
			$lastlogin['time'] = $lastlogin['1'];

			date_default_timezone_set('Asia/Jakarta');
	        $timeNow = date("H:i:s", time());
	        $dateNow = date("Y-m-d", time());

	        if($lastlogin['date'] != $dateNow)
	        	return false;
	        else
	        {
	        	$subtractionTime = ((strtotime($timeNow) - strtotime($lastlogin['time']) ))/60;
	        	if($subtractionTime <=1)
	        		return true;
	        	else
	        		return false;
	        }
		}
	}

	public function getFriendsOnline($user_id)
	{
		$sql='
			SELECT
				friendship.friend_id,`user`.`name`,`user`.avatar,`user`.lastlogin
			FROM
				friendship
			INNER JOIN `user` ON friendship.friend_id = `user`.id
			WHERE
				friendship.`status` = "friend" AND
				friendship.user_id = '.$user_id.'
			ORDER BY `user`.`name`
		';
		$query=Yii::app()->db->createCommand($sql)->queryAll();
		for ($i=0; $i < count($query); $i++)
			$query[$i]['online'] = MessengerController::isOnline($query[$i]['friend_id']);
		return $query;
	}

	public function viewListNotification($user_id)
	{
		$temp = Messenger::model()->viewListNotification($user_id);
		$notifications = array();
		for ($i=0;$i < count($temp);$i++)
		{
			$data =$temp[$i]->attributes;
			//$data['i'] = User::model()->viewIdNameAvatar($data['friend_id']);
			$data['you'] = User::model()->viewIdNameAvatar($data['user_id']);
			$data['posttime'] = NotificationController::getNotificationDateTime($data['posttime']);
			if(isset($notifications[$data['you']['id']]))
			{
				$notifications[$data['you']['id']]['content'][] = $data['content'];
			}
			else
			{
				$data['content'] = array(0 => $data['content']);
				$notifications[$data['you']['id']] = $data;
			}
		}
		return $notifications;
	}
	public function messengerRead($user_id,$friend_id)
	{
		$status = Messenger::model()->messengerRead($user_id,$friend_id);
		if($status == '')
			return 'Cập nhật tin nhắn đã đọc thất bại';
		else
			return '';
	}

	public function getQuantityUnread($user_id)
	{
		$data = Messenger::model()->getQuantityUnread($user_id);
		return count($data);
	}
}